package com.corpdata.system.org.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.corpdata.common.domain.DataGridRequestDTO;
import com.corpdata.common.result.Result;
import com.corpdata.core.base.BaseController;
import com.corpdata.system.org.entity.OrgPermission;
import com.corpdata.system.org.service.OrgPermissionService;

/**
 * 系统权限控制器类
 * @author zealon
 * @date 2018年3月1日
 */
@Controller
@RequestMapping("system/org/permission")
public class OrgPermissionController extends BaseController{
	
	@Autowired
	private OrgPermissionService orgPermissionService;
	
	@RequestMapping("/list")
	public String list(){
		return "system/org/permission/permission_list";
	}
	
	@RequestMapping("/toAdd")
	public String toAdd(){
		return "system/org/permission/permission_add";
	}
	
	@RequestMapping("/toEdit")
	public String toEdit(String id,ModelMap modeMap){
		modeMap.put("record", orgPermissionService.selectById(id));
		return "system/org/permission/permission_edit";
	}
	
	/**
	 * 给ztree插件使用的数据格式
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/gettreejson")
	public String getTreeJson(){
		return orgPermissionService.selectAllTreeJsonData();
	}
	
	/**
	 * 保存权限
	 * @param op
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Result save(OrgPermission op){
		return orgPermissionService.save(op);
	}
	
	/**
	 * 编辑权限
	 * @param op
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/edit")
	public Result edit(OrgPermission op){
		return orgPermissionService.update(op);
	}
	
	/**
	 * 删除权限
	 * @param op
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(String id){
		return orgPermissionService.deleteById(id);
	}
	
	/**
	 * 返回所有权限列表的数据 数据是以list集合形式返回
	 * 这里主要是给layui TreeTable 插件使用的数据格式
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getTreeTableJson")
	public String getTreeTableJson(DataGridRequestDTO dgRequest){
		return orgPermissionService.findByPage(dgRequest);
	}
	
	@ResponseBody
	@RequestMapping("/findRolePermmission")
	public String getPermissionByRole(String roleId){
		String permissionIds=orgPermissionService.selectPermissionIdByRoleId(roleId);
		return "{\"success\":\"true\",\"msg\":\""+permissionIds+"\"}";
	}

}
